<?php
use backend\assets\AgentAsset;
use yii\bootstrap\ActiveForm;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\LinkPager;

AgentAsset::register($this);
?>
<div class="hide"><iframe name='send_post' id='send_post'></iframe></div>
<div>
    <form class="" action="<?=Url::toRoute(['financial/cashier'])?>" method="get">
        <div class="lh45 mg20-y">
            <input class="lh45 mg20-x" type="text" placeholder="输入合作项目名称" name="title" value="<?=\Yii::$app->request->get('title')?>" style="width:224px;">
            <input type="hidden" name="action" value="cooperationprojectslist" />
            <span class="btnBg ml20" style=" height: 40px;">
                <input type="submit"  value="搜索" style="height: 40px;line-height: 40px; background: 0; border: 0; "/>
            </span>
        </div>
    </form>
    <table class="table table-bordered" style="table-layout: fixed;">
        <thead>
            <tr>
                <th>合作项目名称</th>
                <th>原价</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="text-center" colspan="3"><a class="add-recharge" href="javascript:;"><img class="mr10" src="<?=Yii::getAlias('@web/backend/img/financial/addnew.png');?>" alt="" style="margin-right: 10px"/>添加</a></td>
            </tr>
            <?php foreach ($list as $r):?>
            <tr style="cursor: pointer;">
            	<?php ActiveForm::begin(['action'=>Url::toRoute(['financial/cashier','action'=>'updatecooperationprojects']),'options'=>['target'=>'send_post']]);?>
                <td><input class="read-only username" type="text" name="key" value="<?=Html::encode($r['config_key'])?>" readonly/></td>
                <td><input class="read-only sell_price" type="text" name="val" value="<?=Html::encode($r['config_val'])?>" readonly/></td>
                <input type="hidden" name="id" value="<?=$r['config_id']?>" />
                <td>
                    <a class="editBtn btn btn-sm btn-borB" href="javascript:;">编辑</a>
                    <div class="fadeBox" style="display: none;">
                        <input type="submit" class="edit btn btn-sm btn-borB cet" value="保存"/>
                        <a href="#" class="edit rest btn btn-sm btn-borB1">取消</a>
                    </div>
                </td>
                <?php ActiveForm::end();?>
            </tr>
			<?php endforeach;?>
            <tr class="lastTr" style="display: none;">
                <?php ActiveForm::begin(['action'=>Url::toRoute(['financial/cashier','action'=>'addcooperationprojects']),'options'=>['target'=>'send_post']]);?>
                    <td><input id="username" type="text" name="key"></td>
                    <td><input id="sell_price" type="text" name="val"></td>
                    <td>
                        <input type="submit" value="保存" onclick=" return checkSeat()" class="edit btn btn-sm btn-borB" /></a><a href="#" class="edit restb btn btn-sm btn-borB1" style="margin-left: 5px;">取消</a>
                    </td>
                <?php ActiveForm::end();?>
            </tr>
        </tbody>
    </table>
    <div class="page" style="text-align:center;">
                   <?=LinkPager::widget([   
					    'pagination' => $page,
						'firstPageLabel' => '首页',
						'nextPageLabel' => '下一页',
						'prevPageLabel' => '上一页',
						'lastPageLabel' => '末页',
					    'hideOnSinglePage'=>false
					]); ?>
                </div>
</div>

<script>
    $(document).ready(function(){
        var $product_id = document.getElementsByClassName('username');
        $product_price = document.getElementsByClassName('sell_price');
        var $reg = /^(\d{0,9}|[0-9]+\.[0-9]{1,2})$/;
        var $subMit = document.getElementsByClassName('cet');
        for (var i = 0, j = $subMit.length; i < j; i++){
            //scope2 作用域2
            var $set = $subMit[i];
            $set.onclick = function(i){
                //scope3 作用域3
                return function(){
                    //scope4 作用域4
                    if ( i<$product_id.length){
                        var $key = $product_id[i]
                        if ($key.value == 0) {
                            layer.alert('请输入合作项目名称', {
                                icon: 2,
                                shade: 0.2,
                            });
                            return false
                        }
                        else if ($key.value.length > 7) {
                            layer.alert('合作项目名称不能超过7个字', {
                                icon: 2,
                                shade: 0.2,
                            });
                            return false
                        }
                        var $val = $product_price[i];
                        if ($val.value == '') {
                            layer.alert('请输入价格', {
                                icon: 2,
                                shade: 0.2
                            });
                            return false
                        }
                        else if ($val.value !='' && !$reg.test($val.value)) {
                            layer.alert('请输入不超过9位的数字', {
                                icon: 2,
                                shade: 0.2
                            });
                            return false
                        }
                    }
                    console.log($key)
                }
            }(i)
        }
    });
    function checkSeat(){
      var $product_id = $('#username').val()
        $product_price =$('#sell_price').val()
        var $reg = /^(\d{0,9}|[0-9]+\.[0-9]{1,2})$/
        //验证用户名
        if ($product_id ==0){
            layer.alert('请输入合作项目名称', {
                icon: 2,
                shade: 0.2,
            });
            return false
        }
        else if ($product_id.length > 7) {
            layer.alert('合作项目名称不能超过7个字', {
                icon: 2,
                shade: 0.2,
            });
            return false
        }

        //验证价格
        if ($product_price == 0){
            layer.alert('请输入价格', {
                icon: 2,
                shade: 0.2,
            });
            return false
        }
        else if ($product_price !='' && !$reg.test($product_price)) {
            layer.alert('请输入不超过9位的数字', {
                icon: 2,
                shade: 0.2,
            });
            return false
        }
    }
</script>

<?php $this->beginBlock('js_index_end') ?>
$(function(){
$('.table tr').dblclick(function(){
var $this = $(this);
$this.find('input[type="text"]').removeClass('read-only').removeAttr('readonly');
$this.find('.fadeBox').show();
$this.find('.editBtn').hide()
})
$('.rest').click(function(e){
e.stopPropagation();
$(this).parent().hide();
$(this).parent().parent().find('.editBtn').show();
$(this).parent().parent().parent().removeClass('current')
$(this).parent().parent().parent().find('input[type="text"]').addClass('read-only').attr('readonly',true);
})

$('.editBtn').click(function(e){
e.stopPropagation();
$(this).hide();
$(this).next().show();
$(this).parent().parent().addClass('current')
$(this).parent().parent().siblings().removeClass('current')
$(this).parent().parent().find('input[type="text"]').removeClass('read-only').removeAttr('readonly');
})

$('.add-recharge').click(function(e){
e.stopPropagation();
$('.lastTr').show();
$('.lastTr').addClass('current')
})
$('.restb').click(function(e){
e.stopPropagation();
$('.lastTr').hide();
})
$('.lastTr input').click(function(e){
e.stopPropagation();
})
})

<?php $this->endBlock() ?>
<?php $this->registerJs($this->blocks['js_index_end'], $this::POS_END); ?>


<?php $this->registerJsFile("@web/system/financial/cashier/cooper.js",['position'=>$this::POS_END]);?>